package com.handinglian.common.utils;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author pxl
 * @description
 * @date 2019/4/28 16:57
 */
public class DateUtil {
    private static Calendar calendar = Calendar.getInstance();

    public DateUtil() {
    }

    public static String formatDatetime(Date date) {
        return date == null ? "" : (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
    }

    public static String formatDatetimeMinute(Date date) {
        return date == null ? "" : (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(date);
    }

    public static String formatDate(Date date) {
        return date == null ? "" : (new SimpleDateFormat("yyyy-MM-dd")).format(date);
    }

    public static Date now() {
        return new Date();
    }

    public static Integer todayInt() {
        SimpleDateFormat ONLY_DATE_INT = new SimpleDateFormat("yyyyMMdd");
        String todayText = ONLY_DATE_INT.format(now());
        Integer today = new Integer(todayText);
        return today;
    }

    public static String nowDate() {
        SimpleDateFormat ONLY_DATE = new SimpleDateFormat("yyyy-MM-dd");
        return ONLY_DATE.format(new Date());
    }

    public static Date thatDay(Date date) {
        if (date == null) {
            return null;
        } else {
            calendar.setTime(date);
            calendar.set(11, 0);
            calendar.set(12, 0);
            calendar.set(13, 0);
            calendar.set(14, 0);
            return calendar.getTime();
        }
    }

    public static Date nextDay(Date date) {
        calendar.setTime(date);
        calendar.add(5, 1);
        return thatDay(calendar.getTime());
    }

    public static Date getDate(int year, int month, int day) {
        calendar.set(1, year);
        calendar.set(2, month - 1);
        calendar.set(5, day);
        return thatDay(calendar.getTime());
    }

    public static Date tenYearsLater() {
        calendar.add(1, 10);
        return calendar.getTime();
    }

    public static Date after1Hour() {
        calendar.add(10, 1);
        return calendar.getTime();
    }

    public static Date after24Hour() {
        calendar.add(10, 24);
        return calendar.getTime();
    }

    public static Date after1Week() {
        calendar.add(5, 7);
        return calendar.getTime();
    }

    public static Date from(String source) {
        if (StringUtil.isBlank(source)) {
            return null;
        } else {
            try {
                String[] timeParts = source.split(":");
                if (timeParts.length == 1) {
                    return (new SimpleDateFormat("yyyy-MM-dd")).parse(source);
                }

                if (timeParts.length == 2) {
                    return (new SimpleDateFormat("yyyy-MM-dd HH:mm")).parse(source);
                }

                if (timeParts.length == 3) {
                    return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(source);
                }
            } catch (ParseException var2) {
                var2.printStackTrace();
            }

            return null;
        }
    }

    public static String getWeekOfDate(Date dt) {
        String[] weekDays = new String[]{"7", "1", "2", "3", "4", "5", "6"};
        calendar.setTime(dt);
        int w = calendar.get(7) - 1;
        if (w < 0) {
            w = 0;
        }

        return weekDays[w];
    }

    public static Integer parseDateInt(String date10) {
        Integer result = null;

        try {
            SimpleDateFormat DEFAULT = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat DEFAULT_INT = new SimpleDateFormat("yyyyMMdd");
            Date date = DEFAULT.parse(date10);
            result = Integer.parseInt(DEFAULT_INT.format(date));
        } catch (Exception var5) {
            ;
        }

        return result;
    }

    public static String formatDate(Integer date8) {
        String result = "";

        try {
            String dateTime = date8.toString();
            result = dateTime.substring(0, 4) + "-" + dateTime.substring(4, 6) + "-" + dateTime.substring(6);
        } catch (Exception var3) {
            ;
        }

        return result;
    }

    public static int getDateInt(Date date) {
        if (date == null) {
            return getDateInt(new Date());
        } else {
            Integer result = Integer.valueOf(0);

            try {
                SimpleDateFormat DEFAULT_INT = new SimpleDateFormat("yyyyMMdd");
                result = Integer.parseInt(DEFAULT_INT.format(date));
            } catch (Exception var3) {
                ;
            }

            return result.intValue();
        }
    }

    public static long getTenSecondsSpanOneDay(Date date) {
        if (date == null) {
            return 0L;
        } else {
            long timestamp = date.getTime();
            Date today = thatDay(date);
            long timestampToday = today.getTime();
            long result = (timestamp - timestampToday) / 10000L;
            return result;
        }
    }

    public static Date getEndDate(Date endDate) {
        calendar.setTime(endDate);
        calendar.set(10, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        calendar.add(5, 1);
        return calendar.getTime();
    }

    public static Integer firstDateOfThisWeek() {
        return firstDateOfThisWeek(now());
    }

    public static Integer firstDateOfNextWeek() {
        return firstDateOfThisWeek(after1Week());
    }

    public static Integer firstDateOfThisWeek(Date date) {
        calendar.setTime(date);
        int offset = 0;
        switch(calendar.get(7)) {
            case 1:
                offset = -6;
                break;
            case 2:
                offset = 0;
                break;
            case 3:
                offset = -1;
                break;
            case 4:
                offset = -2;
                break;
            case 5:
                offset = -3;
                break;
            case 6:
                offset = -4;
                break;
            case 7:
                offset = -5;
        }

        calendar.add(5, offset);
        Integer result = getDateInt(calendar.getTime());
        return result;
    }

    public static Integer firstDateOfThisMonth() {
        return firstDateOfThisMonth(now());
    }

    public static Integer firstDateOfNextMonth() {
        calendar.add(2, 1);
        calendar.set(5, 1);
        return calendar.get(1) * 10000 + (calendar.get(2) + 1) * 100 + calendar.get(5);
    }

    public static Integer firstDateOfThisMonth(Date date) {
        calendar.setTime(date);
        int year = calendar.get(1);
        int month = calendar.get(2) + 1;
        Integer result = (year * 100 + month) * 100 + 1;
        return result;
    }

    public static Date getAfter1Week(Date date) {
        calendar.setTime(date);
        calendar.add(5, 7);
        return calendar.getTime();
    }

    public static Date getAfter1Month(Date date) {
        calendar.setTime(date);
        calendar.add(2, 1);
        calendar.set(5, 1);
        return calendar.getTime();
    }

    /**
     * 获取星期几
     * @author pxl
     * @date 2019/1/30 16:07
     * @param date
     * @return int
     **/
    public static int getWeek(Date date){
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK)-1;
    }

    /**
     * 获取当前年份
     * @author pxl
     * @date 2019/2/20 11:06
     * @param date
     * @return int
     **/
    public static int getYear(Date date){
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    public static int getMonth(Date date){
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH)+1;
    }

    /**
     * @param dealDate '2013-05'
     * @return 指定月天数
     */
    public static int getDayOfMonth(String dealDate) {
        Date date = parseDate(dealDate, "yyyy-MM");
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static Date getFirstDayOfYear(Date date){
        calendar.setTime(date);
        //获取本月第一天
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return calendar.getTime();
    }

    public static Date getFirstDayOfMonth(Date date){
        calendar.setTime(date);
        //获取本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    public static Date getFirstDayOfWeek(Date date){
        calendar.setTime(date);
        //获取本周一的日期
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return calendar.getTime();
    }

    public static Date getFirstDayOfLastWeek(Date date){//获取下周第一天
        return addDays(getFirstDayOfWeek(date),7);
    }

    /**
    * 增加天数
    * @author pxl
    * @param date
    * @param days
    * @return java.util.Date
    * @date 2019/8/29 11:11
    */
    public static Date addDays(Date date, Integer days){
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    /**
    * 增加秒数
    * @author pxl
    * @param date
    * @param seconds
    * @return java.util.Date
    * @date 2019/8/29 11:13
    */
    public static Date addSeconds(Date date, Integer seconds){
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, seconds);
        return calendar.getTime();
    }

    public static void main(String[] args) {
        calendar.setTime(null);
        System.out.println(calendar);
    }

    /**
     *
     * @param starTime
     * @param endTime
     * @return 2段时间差
     */
    public static StringBuffer getUserTime(Date starTime, Date endTime) {
        StringBuffer userTimeStr=new StringBuffer();
        Long nd = 1000L * 24 * 60 * 60;
        Long nh = 1000L * 60 * 60;
        Long nm = 1000L * 60;
        Long userTimes=endTime.getTime()-starTime.getTime();
        long day = userTimes / nd;
        // 计算差多少小时
        long hour = userTimes % nd / nh;
        // 计算差多少分钟
        long min = userTimes % nd % nh / nm;
        userTimeStr.append(day).append("天").append(hour).append("时").append(min).append("分");
        return userTimeStr;
    }

    /**
     * 计算两个时间之间相隔天数
     * @author pxl
     * @date 2018/11/19 11:19
     * @param startDate
     * @param endDate
     * @return int
     **/
    public static int getIntervalDays(Date startDate,Date endDate){
        //确保startDate在endDate之前
        if(startDate.after(endDate)){
            Date date=startDate;
            startDate=endDate;
            endDate=date;
        }
        //分别得到两个时间的毫秒数
        long sl=startDate.getTime();
        long el=endDate.getTime();

        long ei=el-sl;
        //根据毫秒数计算间隔天数
        return (int)(ei/(1000*60*60*24));
    }

    /**
     * 获取时间天数差， 区分前后，可能返回负数
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getActIntervalDays(Date startDate,Date endDate){
        //分别得到两个时间的毫秒数
        long sl=startDate.getTime();
        long el=endDate.getTime();

        long ei=el-sl;
        //根据毫秒数计算间隔天数
        return (int)(ei/(1000*60*60*24));
    }

    public static int getIntervalYear(Date startDate, Date endDate)
    {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(startDate);
        c2.setTime(endDate);
        return (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) < 0 ? 0 : c2
                .get(Calendar.YEAR)
                - c1.get(Calendar.YEAR);
    }

    /**
     * 解析字符串为Date对象<br>
     * 如果格式不符合要求或解析错误将返回null
     *
     * @param date
     * @param format
     *            字符串格式: yyyy-MM-dd
     * @return Date or null
     */
    public static Date parseDate(String date, String format) {
        Date d = null;
        if (StringUtils.isEmpty(date)) {
            return d;
        }
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            d = df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            d = null;
        }
        return d;
    }
}
